package crawl.http;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import crawl.CrawlingException;
import crawl.ProcessThread;

import utils.Logger;
import utils.Util;

public class HtmlCrawlProcessThread extends ProcessThread
{
    public void run()
    {
        try
        {
            calcPageRank();
            buildDatabase();
        }
        catch (Exception e)
        {
            Logger.logException(e, true);
        }
    }

    private void buildDatabase() throws IOException
    {
        HtmlIndexer indexer = new HtmlIndexer();
        indexer.build();
    }

    private void calcPageRank() throws CrawlingException
    {
        if (this.su == null)
            throw new CrawlingException("parameter Crawler is null:"
                    + " should use \"start(Crawler su)\" to start this thread");
        BufferedWriter writer;
        try
        {
            writer = new BufferedWriter(new FileWriter(Util.getResultFile(),
                    true));
            writer.write(Util.getLineSeparator() + "Computing the PageRank..."
                    + Util.getLineSeparator());
            PageGraph graph = PageGraph.getInstance();
            Set<String> validateURI = this.su.getCrawledURI();
            Set<String> deleteURI = new HashSet<String>();
            for (String str : graph.getUrls())
            {
                if (!validateURI.contains(str))
                    deleteURI.add(str);
            }
            for (String str : deleteURI)
            {
                graph.delete(str);
            }
            for (Page p : graph.getPages())
            {
                p.setDocumentID(this.su.getDocumentID(p.getUrl()));
            }
            graph.calcPageRank(writer, 1e-5, 0.8);
            writer.flush();
            writer.close();
        }
        catch (IOException e)
        {
            Logger.logException(e);
        }
    }
}
